package com.topc.ucenter.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.topc.base.constant.RedisConstant;
import com.topc.base.exception.Asserts;
import com.topc.base.model.UserAuthDto;
import com.topc.core.sevice.RedisService;
import com.topc.ucenter.mapper.*;
import com.topc.ucenter.model.dto.AuthParamsDto;
import com.topc.ucenter.model.po.AdminInfo;
import com.topc.ucenter.model.po.RoleInfo;
import com.topc.ucenter.model.po.UserInfo;
import com.topc.ucenter.service.IAuthService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("password_auth_service")
public class PasswordAuthServiceImpl implements IAuthService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private CourseInfoMapper courseInfoMapper;

    @Autowired
    private CourseUserMidMapper courseUserMidMapper;

    @Autowired
    private RoleInfoMapper roleInfoMapper;

    @Autowired
    private RoleMenuMidMapper roleMenuMidMapper;

    @Autowired
    private AdminInfoMapper adminInfoMapper;


   @Autowired
   private PasswordEncoder passwordEncoder;


    @Override
    public UserAuthDto execute(AuthParamsDto authParamsDto) {
        String number = authParamsDto.getNumber();
//        Object isLock = redisService.hGet(RedisConstant.PEOPLE_LOCK_NUMBER, authParamsDto.getOauthEndpoint() + "-" + authParamsDto.getNumber());
//        if (ObjectUtil.isNotEmpty(isLock)) Asserts.fail("账号正在检查中,目前在锁定中,锁定结束将会用邮箱提醒,谢谢配合");
        String password = null;
        Long roleId = null;
        String salt = null;
        UserInfo userInfo = null;
        AdminInfo adminInfo = null;
        Integer roleType = null;
        UserAuthDto authDto = new UserAuthDto();
        if (ObjectUtil.equal("user", authParamsDto.getOauthEndpoint())) {
            userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getNumber, number));
            if (ObjectUtil.isEmpty(userInfo)) Asserts.fail("未查询到用户信息");
            if (userInfo.getStatus()==1) Asserts.fail("用户已锁定");
            password = userInfo.getPassword();
            roleId = userInfo.getRoleId();
            salt = userInfo.getSalt();
            roleType = 2;
            authDto.setJoinCourses(courseUserMidMapper.getJoinCourseIdsByUserNumber(number))
                    .setManagerCourses(courseInfoMapper.getCourseIdsByUserNumber(number));
            System.out.println("登录用户 " + userInfo.getUsername());
        } else if (ObjectUtil.equal("admin", authParamsDto.getOauthEndpoint())) {
            adminInfo = adminInfoMapper.selectOne(new LambdaQueryWrapper<AdminInfo>().eq(AdminInfo::getNumber, number));
            if (ObjectUtil.isEmpty(adminInfo)) Asserts.fail("未查询到用户信息");
            if (adminInfo.getStatus()==1) Asserts.fail("用户已锁定");
            password = adminInfo.getPassword();
            roleId = adminInfo.getRoleId();
            salt = adminInfo.getSalt();
            roleType = 1;
            System.out.println("登录用户 " + adminInfo.getUsername());
        } else {
            Asserts.fail("请选择正确的客户端进入");
        }
        boolean matches = passwordEncoder.matches(authParamsDto.getPassword(), password);
        if (!matches) Asserts.fail("账号密码不匹配");
        RoleInfo roleInfo = roleInfoMapper.selectById(roleId);
        String roleCode = roleInfo.getRoleCode();
        String[] authMenuCodes = roleMenuMidMapper.getMenuCodeByRoleId(roleInfo.getId());
        authDto.setRole(roleCode).setNumber(number).setSalt(salt).setMenuCodes(authMenuCodes);
        System.out.println(authDto.getRole());
        return authDto;
    }

}
